<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of NavHelper
 *
 * @author kri
 */
class NavHelper extends AppHelper {

    public $name = "Nav";
    public $helpers = array('Html', 'Session');
    private $userController = array(        
        array(
            'title' => 'Übersicht',
            'url' => array('controller' => 'bookings', 'action' => 'calendar')
        ),
        array(
            'title' => 'Buchungen',
            'url' => array('controller' => 'bookings', 'action' => 'index')
        ),
        array(
            'title' => 'Räume',
            'url' => array('controller' => 'rooms', 'action' => 'index')
        ),
    );
    
    private $logactions = array(
        array(
            'title' => 'Log out',
            'url' => array('controller' => 'users', 'action' => 'logout')
        )
    );


    private $adminController = array(
        array(
            'title' => 'Ausstattung',
            'url' => array('controller' => 'features', 'action' => 'index')
        )        
    );
    
    private $userActions = array(
        array(
            'title' => 'index',
            'url' => array('action' => 'index')
        ),
        array(
            'title' => 'add',
            'url' => array('action' => 'add')
        ),
    );
    
    public function main($context) {
        if ($context == 'main'){            
            if ($this->Session->read('is_admin') == true){
                $this->userController = array_merge($this->userController, $this->adminController);
            }
            $this->userController = array_merge($this->userController, $this->logactions);
            return '<nav class="nav nav-tabs nav-justified" role="tablist">' . $this->nav($this->userController, $context) . '</nav>';            
        }    
        if ($context == 'actions'){            
            return '<nav class="nav nav-pills nav-justified">' . $this->nav($this->userActions , $context) . '</nav>';            
        }        	
    }

    private function nav($items, $context) {
        $content = '';
        if ($context == 'main'){
            foreach ($items as $item) {
                $content .= '<li '.$this->isGroup($item).'>'.$this->Html->link($item['title'], $url = $this->getUrl($item)).'</li>';
            }
        } else{
            foreach ($items as $item) {
                $content .= '<li '.$this->isActive($item).'>'.$this->Html->link($item['title'], $url = $this->getUrl($item)).'</li>';
            }
        }
        return $content;
    }

    private function getUrl($item) {
        $url = false;
        if (isset($item['url'])) {
            $url = $item['url'];
        }

        return $url;
    }

    private function isActive($item) {
        $url = $this->Html->url($this->getUrl($item));  
        if ($this->here == $url){
            $active = 'class="active"';
        } else {
            $active ='';
        }
        return $active;        
    }
    
    private function isGroup($item){        
        //Want the controller only!
        $url = explode('/', $this->Html->url($this->getUrl($item)));
        $actControlle = explode('/', $this->here);
        if ($actControlle[2] == $url[2]) {
            $active = 'class="active"';
        } else {
            $active ='';
        }
        return $active; 
    }

}
